<?php
/*
	Convert H:m => H:m PM/AM
*/
function time_label( $str ){
	return strtoupper( date("g:i a",strtotime("2011/01/01 $str")) );
}

function date_state( $date ){
	$date = date('d-m-Y',strtotime($date));
	if( $date == date('d-m-Y',time()) ){
		return "hôm nay";
	}else{
		return $date;
	}
}

/*
	Convert a date from dd/mm/yyyy => yyyy-mm-dd
*/
function change_date_format( $date ){	
	if( preg_match("#(\d{1,2})\D+(\d{1,2})\D+(\d{4})#is", $date, $matches ) ){
		$d = (int)$matches[1];
		$m = (int)$matches[2];
		$y = (int)$matches[3];
		return $y.'-'.( $m < 10 ? "0$m" : $m ).'-'.( $d < 10 ? "0$d" : $d );
	}
	return $date;
}

function change_datetime_format( $date ){
	if( preg_match("#(\d{1,2})\D+(\d{1,2})\D+(\d{4}) (\d{1,2})\D+(\d{1,2})\D+(\d{1,2})#is", $date, $matches ) ){
		$d = (int)$matches[1];
		$m = (int)$matches[2];
		$y = (int)$matches[3];

		$H = (int)$matches[3];
		$i = (int)$matches[4];
		$s = (int)$matches[5];
		
		return $y.'-'.( $m < 9 ? "0$m" : $m ).'-'.( $d < 9 ? "0$d" : $d )
			." $H:$i:$s";
	}
	return $date;
}

/* date for mysql */
function mysql_date( $time = NULL ){
	return $time == NULL 
		? date('Y-m-d H:i:s') : date('Y-m-d H:i:s',$time);
}
?>